//
//  BIDFavorableInfoViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/12.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDFavorableInfoListViewController.h"
#import "BIDAddFavorableInfoViewController.h"
#import "BIDFavorableInfoView.h"

static NSString *favorableInfoListURL = @"preferentialAndroid/list.action";

@interface BIDFavorableInfoListViewController ()<BIDBaseListViewDelegate>
{
    BIDFavorableInfoView *_tempFavorableInfoView;
}
@end

@implementation BIDFavorableInfoListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"优惠信息";
    // Do any additional setup after loading the view.
    //获取当前用户是否有发布优惠信息的权限
    self.myTableView.allowsSelection = NO;
    NSDictionary *userInfoDic = [AppDelegate getUserDetailInfo];
    NSNumber *isHaveAuthorizeValue = userInfoDic[@"isAuthorize"];
    //if([isHaveAuthorizeValue integerValue] == 1)
    {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addFavorableInfoHandler)];
    }
    _tempFavorableInfoView = [[[NSBundle mainBundle] loadNibNamed:@"BIDFavorableInfoView" owner:self options:nil] lastObject];
    //
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.curPage = 0;
    [self.dataSourceArr removeAllObjects];
    [self loadData];
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], favorableInfoListURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"page=%ld&rows=%ld", self.curPage, self.countPerPage];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            NSArray *arr = responseDic[@"rows"];
            if(arr.count>0)
            {
                [self.dataSourceArr addObjectsFromArray:arr];
                if(self.dataSourceArr.count < self.curPage*self.countPerPage)
                {
                    [self.myTableView hasNoMoreData];
                }
                else
                {
                    [self.myTableView hasMoreData];
                }
                [self.myTableView reloadData];
            }
        }
        [self autoDecideToShowEmptyView];
    }];
}

/**
 发布新的优惠信息
 */
- (void)addFavorableInfoHandler
{
    BIDAddFavorableInfoViewController *vc = [[BIDAddFavorableInfoViewController alloc] initWithNibName:@"BIDAddFavorableInfoViewController" bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    [self setNavigationControllerAttribute:nav];
    [self presentViewController:nav animated:YES completion:^{}];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSDictionary *dic = self.dataSourceArr[row];
    BIDFavorableInfoView *infoView = [cell.contentView viewWithTag:100];
    if(infoView)
    {
        [infoView removeFromSuperview];
    }
    //else
    {
        infoView = [[[NSBundle mainBundle] loadNibNamed:@"BIDFavorableInfoView" owner:self options:nil] lastObject];
        infoView.tag = 100;
        [infoView adjustLayoutWithDic:dic];
        CGRect frame = infoView.frame;
        frame.size.width = CGRectGetWidth(tableView.frame);
        infoView.frame = frame;
        [cell.contentView addSubview:infoView];
    }
    cell.contentView.autoresizesSubviews = NO;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = indexPath.row;
    NSDictionary *dic = self.dataSourceArr[row];
    CGFloat height = [_tempFavorableInfoView adjustLayoutWithDic:dic];
    return height+1;
}

@end
